package pe.gob.sedapal.scmultimedia.rcp.action;



import org.eclipse.jface.action.Action;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.jpedal.PdfDecoder;
import pe.gob.sedapal.scmultimedia.rcp.view.servicios.PdfView;

public class MovePageAction extends Action implements IWorkbenchAction {
	private static final String ID = "pe.gob.sedapal.scmultimedia.rcp.action.MovePageAction";  
	
	private PdfDecoder decodePDF;
	private int currentPage;
	private int page;
	private PdfView vista;
	
	public MovePageAction(int page,PdfView vista){  
		setId(ID);  
		this.page=page;
		this.vista = vista;
	}  
	
	public MovePageAction(){  
	}  
	
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}
	@Override
	public void run() {
		PdfView view=(PdfView)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(PdfView.ID);  
		decodePDF=view.getDecodePDF();		
		currentPage=view.getCurrentPage();
		 
		movePage(view);
	}
	
	private void movePage(PdfView view) {
        try {
        	int temp=currentPage+page;
        	System.out.println("currentPage"+currentPage);
    		setCurrentPage(currentPage);
        	System.out.println("page"+page);

       
        	if(temp > decodePDF.getPageCount()){
        		temp = decodePDF.getPageCount();
        	}else if(temp <1){
        		temp=1;
        	}
        	if (temp!=currentPage){
        		view.setCurrentPage(temp);
        		decodePDF.decodePage(temp);
    			decodePDF.updateUI();
    			view.repaint();
        	} 
         	vista.actualizarPagina(view.getCurrentPage());
        	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	

	


}
